package com.yazi.mxz.person.controller;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.hibernate.SearchingFactory;
import com.myframework.smhj.hibernate.Sorting;
import com.myframework.smhj.jackson.result.ListResult;
import com.myframework.smhj.jackson.result.ObjectResult;
import com.yazi.mxz.common.entity.beans.Category;
import com.yazi.mxz.common.service.CategoryService;
import com.yazi.mxz.common.service.StoreService;
import com.yazi.mxz.person.jackson.filter.CategoryFilter;

@Controller("categoryController")
@RequestMapping("/category")
public class CategoryController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(CategoryController.class);

	@Resource
	CategoryService categoryService;
	@Resource
	StoreService storeService;

	@RequestMapping("/listWithSons/json")
	@ResponseBody
	public ListResult<?> listWithSonsJson(Category category) {
		log.debug("获取类目列表数据");
		ListResult<Category> jsonResult = new ListResult<Category>();
		jsonResult.setFilter(CategoryFilter.getWithCategories());
		try {
			Sorting sorting = SearchingFactory.createSorting("sort", true);
			List<Category> list = categoryService.list(category, sorting);
			jsonResult.setResult(list);
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("获取类目列表异常:", e);
			jsonResult.setErrorMessage("加载失败!");
		}
		return jsonResult;
	}

	@RequestMapping("/listSons/json")
	@ResponseBody
	public ObjectResult<?> listSonsJson(Long catId) {
		log.debug("获取子类目列表:catId={}", catId);
		ObjectResult<Category> jsonResult = new ObjectResult<Category>();
		jsonResult.setFilter(CategoryFilter.getWithCategories());
		try {
			Category parent = categoryService.one(catId);
			jsonResult.setResult(parent);
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("获取子分类列表异常:", e);
			jsonResult.setErrorMessage("加载失败!");
		}
		return jsonResult;
	}

	@RequestMapping("/listBrothers/json")
	@ResponseBody
	public ObjectResult<?> listBrothersJson(Long catId) {
		log.debug("获取兄弟类目列表:catId={}", catId);
		ObjectResult<Category> jsonResult = new ObjectResult<Category>();
		jsonResult.setFilter(CategoryFilter.getWithCategories());
		try {
			Category category = categoryService.one(catId);
			if (null != category.getCategory()) {
				jsonResult.setResult(category.getCategory());
				jsonResult.setOk();
			} else {
				return listTypeRootsJson(-1);
			}
		} catch (Exception e) {
			log.error("获取兄弟类目列表异常:", e);
			jsonResult.setErrorMessage("加载失败!");
		}
		return jsonResult;
	}

	@RequestMapping("/listTypeRoots/json")
	@ResponseBody
	public ObjectResult<?> listTypeRootsJson(Integer type) {
		log.debug("获取根类目列表:type={}", type);
		ObjectResult<Category> jsonResult = new ObjectResult<Category>();
		jsonResult.setFilter(CategoryFilter.getWithCategories());
		try {
			
			Sorting sorting = SearchingFactory.createSorting("sort", true);

			Category cat4s = new Category();
			cat4s.setDeep(0);
			if (null != type && type >= 0) {
				// 某类型根的子一级分类列表
				cat4s.setType(type);
				List<Category> rootList = categoryService.list(cat4s, sorting);
				jsonResult.setResult(rootList.get(0));
			} else {
				// 根列表
				List<Category> rootList = categoryService.list(cat4s, sorting);
				Category interim = new Category();
				Set<Category> sets = new HashSet<Category>();
				sets.addAll(rootList);
				interim.setCategories(sets);
				jsonResult.setResult(interim);
			}
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("获取根类目列表异常:", e);
			jsonResult.setErrorMessage("加载失败!");
		}
		return jsonResult;
	}

}
